package com.young.ysepay.service.impl;

import com.alibaba.fastjson.JSON;
import com.young.ysepay.dto.YsPayDTO;
import com.young.ysepay.service.YsBalanceQueryService;
import com.young.ysepay.service.YsBuildRequestParaService;
import com.young.ysepay.service.YsBusinessPramService;
import com.young.ysepay.service.YsVerifyJsonSignService;
import com.young.ysepay.utils.DateFormatHelper;
import com.young.ysepay.utils.Https;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: WANGNING
 * @Email wyn20140226@aliyun.com
 * @Date: 2019/6/5 8:48
 * @Des
 */
@Service
public class YsBalanceQueryServiceImpl implements YsBalanceQueryService {
    @Autowired
    private YsBusinessPramService ysBusinessPramService;
    @Autowired
    private YsBuildRequestParaService ysBuildRequestParaService;
    @Autowired
    private YsVerifyJsonSignService ysVerifyJsonSignService;
    private static Logger log = LoggerFactory.getLogger(YsBalanceQueryServiceImpl.class);
    @Override
    public String ysBalanceQuery(Map<String, Object> paramMap) {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        System.out.println("银盛商户余额查询接口调用开始======================");
        YsPayDTO ysPayDTO=ysBusinessPramService.ysBusinessPram();
        Object temp = paramMap.get("biz_content");
        String bizContent = temp != null ? temp.toString(): null;

        Map<String, String> param = new HashMap<String, String>();
        //银盛商户余额查询接口
        param.put("method", "ysepay.merchant.balance.query");
        param.put("partner_id", ysPayDTO.getPARTNER_ID());
        param.put("timestamp", DateFormatHelper.getNowTimeStr());
        param.put("charset", ysPayDTO.getDEFAULT_CHARSET());
        param.put("sign_type", ysPayDTO.getSIGN_ALGORITHM());
        param.put("version", ysPayDTO.getVERSION());
        param.put("biz_content", bizContent);
        String str = "";
        try {
            //签名
            Map<String, String> sPara = ysBuildRequestParaService.buildRequestPara(param);
            log.error("银盛商户余额接口参数："+sPara.toString());
            String returnString = Https.httpsSend(ysPayDTO.getYSEPAY_GATEWAY_URL_DSF(), sPara);
            log.error("银盛商户余额接口返回数据："+returnString);
            resultMap = (Map) JSON.parse(returnString);
            Object obj = resultMap.get("ysepay_merchant_balance_query_response");
            boolean verifyResult = false;
            if (obj != null) {
                //Map<String, Object> ysepay_merchant_balance_query_response = (Map) JSON.parseObject(obj.toString());
                int ss = returnString.toString().indexOf("ysepay_merchant_balance_query_response");
                str = returnString.substring(ss);
                str = str.replace("ysepay_merchant_balance_query_response", "");
                str = str.substring(2, str.length() - 1);
                verifyResult = ysVerifyJsonSignService.verifyJsonSign(resultMap.get("sign").toString(), str, "utf-8");
                log.error("验证签名信息"+verifyResult);
                log.error("余额查询信息返回"+str);
                return str;
            }
        } catch (IOException e) {
            log.error("\r\n 银盛商户余额接口错误："+e.getMessage());
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (IllegalAccessException e1) {
            System.out.println(e1.getMessage());
            e1.printStackTrace();
            log.error("\r\n 银盛商户余额接口错误："+e1.getMessage());
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            log.error("\r\n 银盛商户余额接口错误："+e.getMessage());
        } finally {
            return str;
        }
    }
}
